/*
 * Provides methods to limit the input error of the end user.
 * Methods:
 *      Digit(): Allows only to enter digits and control keys.
 *      DigitLetter(): Allows only to enter letters, digits and control keys.
 *      toUpperCase(): Convert lower case into upper case.
 * and open the template in the editor.
 */
package healthcaresystem;

import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.Toolkit;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

public class InputRules implements KeyListener {

    @Override
    public void keyTyped(KeyEvent ke) {
        char c = ke.getKeyChar();
        if (!((c >= '0') && (c <= '9')
                || (c == KeyEvent.VK_BACK_SPACE)
                || (c == KeyEvent.VK_DELETE))) {
            ke.consume();
        }
    }

    @Override
    public void keyPressed(KeyEvent ke) {
    }

    @Override
    public void keyReleased(KeyEvent ke) {
    }
}

class Decimal implements KeyListener {

    @Override
    public void keyTyped(KeyEvent ke) {
        char c = ke.getKeyChar();
        if (!((c >= '0') && (c <= '9') || (c == '.')
                || (c == KeyEvent.VK_BACK_SPACE)
                || (c == KeyEvent.VK_DELETE))) {
            ke.consume();
        }
    }

    @Override
    public void keyPressed(KeyEvent ke) {
    }

    @Override
    public void keyReleased(KeyEvent ke) {
    }
}

/**
 * KeyListener for User Log-in Number:
 * only letters (convert to uppercase), digits and control keys are allowed.
 * MAX_LENGTH = 20
 * How to Use: text1.addKeyListener(new LogIn());
 */
class LogIn implements KeyListener {

    public static int MAX_LENGTH = 20;

    @Override
    public void keyTyped(KeyEvent ke) {
        char c = ke.getKeyChar();
        if ((c >= 'a') && (c <= 'z')) {
            ke.setKeyChar((char) ((int) c + (int) ('A' - 'a')));  //convert to uppercase
        } else if (!((c >= '0') && (c <= '9')
                || (c >= 'A') && (c <= 'Z')
                || (c == KeyEvent.VK_BACK_SPACE)
                || (c == KeyEvent.VK_DELETE))) {
            ke.consume();
        }
    }

    @Override
    public void keyPressed(KeyEvent ke) {
    }

    @Override
    public void keyReleased(KeyEvent ke) {
    }
}
//
//class CapLKOn implements KeyListener {
//
//    @Override
//    public void keyTyped(KeyEvent ke) {
//        JPasswordField pwd = (JPasswordField)ke.getSource(); //get the source object
//        boolean isOn = Toolkit.getDefaultToolkit().getLockingKeyState(KeyEvent.VK_CAPS_LOCK);
//        if (isOn){
//            pwd.setToolTipText("The CapsLock is on");
//        }else
//            pwd.setToolTipText(null);
//    }
//
//    @Override
//    public void keyPressed(KeyEvent ke) {
//    }
//
//    @Override
//    public void keyReleased(KeyEvent ke) {
//    }
//}
